#include "stdafx.h"
#include "InputSystem.h"

using namespace MomogenkyouSystem;

BYTE				InputSystem::KeyboardKeys[256];
BYTE				InputSystem::PrevKeyboardKeys[256];
DIMOUSESTATE2		InputSystem::MouseState;
DIMOUSESTATE2		InputSystem::PrevMouseState;
POINT				InputSystem::MousePosition;
POINT				InputSystem::PrevMousePosition;

LPDIRECTINPUT		InputSystem::DeviceManager(NULL);
LPDIRECTINPUTDEVICE	InputSystem::KeyboardDevice(NULL);
LPDIRECTINPUTDEVICE	InputSystem::MouseDevice(NULL);


bool	InputSystem::Initialize(const HWND		&	param_WindowHandle, 
								const HINSTANCE &	param_WindowInstance)
{
	memset(KeyboardKeys,		0x00,	256);
	memset(PrevKeyboardKeys,	0x00,	256);

	memset(&MouseState,			0x00,	sizeof(DIMOUSESTATE2));
	memset(&PrevMouseState,		0x00,	sizeof(DIMOUSESTATE2));

	memset(&MousePosition,		0x00,	sizeof(POINT));
	memset(&PrevMousePosition,	0x00,	sizeof(POINT));

	HRESULT	_Result = S_OK;
	//	Device Creation
	_Result	=	DirectInput8Create(param_WindowInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&DeviceManager, NULL);
	ASSERT(_Result == S_OK, "Error creating DirectInput DeviceManager");

	_Result = DeviceManager->CreateDevice(GUID_SysKeyboard, &KeyboardDevice, NULL);
	ASSERT(_Result == S_OK, "Error creating DirectInput Keyboard");

	_Result = DeviceManager->CreateDevice(GUID_SysMouse, &MouseDevice, NULL);
	ASSERT(_Result == S_OK, "Error creating DirectInput Mouse");

	//	Keyboard setup
	_Result = KeyboardDevice->SetDataFormat(&c_dfDIKeyboard);
	ASSERT(_Result == S_OK, "Error setting Data Format for Keyboard");

	_Result = KeyboardDevice->SetCooperativeLevel(param_WindowHandle, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
	ASSERT(_Result == S_OK, "Error setting Data Format for Keyboard");

	//	Mouse setup
	_Result = MouseDevice->SetDataFormat(&c_dfDIMouse2);
	ASSERT(_Result == S_OK, "Error setting Data Format for Keyboard");

	_Result = MouseDevice->SetCooperativeLevel(param_WindowHandle, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
	ASSERT(_Result == S_OK, "Error setting Data Format for Keyboard");
	
	//	Retreieve mouse capabilities
	/*LPDIDEVCAPS	_Capabilities;
	_Capabilities->dwSize = sizeof(LPDIDEVCAPS);
	MouseDevice->GetCapabilities(_Capabilities);*/

	//	Acquire Devices
	_Result = KeyboardDevice->Acquire();
	ASSERT(_Result == S_OK, "Error Acquiring Keyboard");
	_Result = MouseDevice->Acquire();
	ASSERT(_Result == S_OK, "Error Acquiring Mouse");


	WriteCommandLine("> InputSystem >> Initialized");
	return true;
}

bool	InputSystem::Shutdown()
{	
	KeyboardDevice->Unacquire();
	MouseDevice->Unacquire();

	KeyboardDevice->Release();
	MouseDevice->Release();
	DeviceManager->Release();
	
	WriteCommandLine("> InputSystem >> Shutdown");
	return true;
}

void	InputSystem::Update(const float & fInterval)
{
	fInterval;
	HRESULT	_Result = S_OK;

	memcpy(PrevKeyboardKeys, KeyboardKeys, 256);
	memcpy(&PrevMouseState, &MouseState, sizeof(DIMOUSESTATE2));
	memcpy(&PrevMousePosition, &MousePosition, sizeof(POINT));

	_Result	=	KeyboardDevice->GetDeviceState(sizeof(KeyboardKeys), &KeyboardKeys);
	ASSERT(_Result == S_OK, "Unable to retrieve keyboard device state");

	_Result	=	MouseDevice->GetDeviceState(sizeof(MouseState), &MouseState);
	ASSERT(_Result == S_OK, "Unable to retrieve keyboard device state");

	GetCursorPos(&MousePosition);
}